//
//  UIViewController+lvfjHelper.m
//  lvfj
//
//  Created by 吕飞俊 on 15/10/14.
//  Copyright © 2015年 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "UIViewController+lvfjHelper.h"

@implementation UIViewController (lvfjHelper)

+(UIViewController *)getStoryBoardWithStoryBoardName:(NSString *)storyboardName
                                          identifier:(NSString *)identifier{
    UIViewController *ctl = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:identifier];
    return ctl;
}

+(UIView *)getNibViewWithNibName:(NSString *)nibName
                           index:(NSInteger)index{
    UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
    NSArray *viewArray = [nib instantiateWithOwner:self options:nil];
    id view = [viewArray objectAtIndex:index];
    return view;
}

+(UIView *)getNibViewFirstWithNibName:(NSString *)nibName{
    return [UIViewController getNibViewWithNibName:nibName index:0];
}

+(id)getNibWithNibName:(NSString *)nibName
                 index:(NSInteger)index{
    UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
    NSArray *viewArray = [nib instantiateWithOwner:self options:nil];
    id view = [viewArray objectAtIndex:index];
    return view;
}

+(id)getNibFirstWithNibName:(NSString *)nibName{
    return [UIViewController getNibWithNibName:nibName index:0];
}

-(void)removeCurrentCtlFromeNavCtlStack:(UIViewController *)ctl{
    NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
    if (array.count < 2) {
        return;
    }
    [array removeLastObject];
    [array addObject:ctl];
    [self.navigationController setViewControllers:array animated:YES];
}

-(void)removeCtl:(NSInteger)number fromeNavCtlStack:(UIViewController *)ctl{
    NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
    if (array.count < number+1) {
        return;
    }
    for (int i = 0; i < number; i++) {
        [array removeLastObject];
    }
    [array addObject:ctl];
    [self.navigationController setViewControllers:array animated:YES];
}

-(void)dismissOrPopView:(BOOL)animation
{
    if (self.navigationController && self.navigationController.viewControllers.count>1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
